﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WMSADL.SY;
using WMSAML.SY;

namespace WMSABL.SY
{
    public class TUserRoleRelation_BLL
    {
        TUserRoleRelation_DAL dal = new TUserRoleRelation_DAL();
         
        /// <summary>
        /// 
        /// </summary>
        public int Add(TUserRoleRelation_Entity entity)
        {
            return dal.Add(entity);
        }

        /// <summary>
        /// 
        /// </summary>
        public bool Delete(int uid)
        {
            return dal.Delete(uid);
        }

        /// <summary>
        /// 
        /// </summary>
        public bool Delete(int uid, int rid)
        {
            return dal.Delete(uid, rid);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TUserRoleRelation_Entity entity)
        {
            return dal.Update(entity);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        public bool UpdateUserRoleRelation(int uid, string ridStr)
        {
            int result = 0;
            if (dal.ExistsByUID(uid))
            {
                this.Delete(uid);
            }
            if (ridStr == "")
            {
                return true;
            }
            string[] arr = ridStr.Split(',');
            TUserRoleRelation_Entity enity = new TUserRoleRelation_Entity();
            enity.User_id = uid;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                enity.Role_id = Convert.ToInt32(arr[i]);
                if (Add(enity) > 0) result++;
            }
            if (result > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// ByID
        /// </summary>
        public bool ExistsByID(int id)
        {
            return dal.ExistsByID(id);
        }

        /// <summary>
        /// ByID
        /// </summary>
        /// <summary>
        public TUserRoleRelation_Entity GetEntityByID(int id)
        {
            return dal.GetEntityByID(id);
        }

        /// <summary>
        /// By uid, int rid 
        /// </summary>
        /// <summary>
        public TUserRoleRelation_Entity GetEntityByID(int uid, int rid)
        {
            return dal.GetEntityByID(uid, rid);
        }
    }
}
